#!/usr/bin/env python
# Create: 2018/10/23
__author__ = '749B'
"""asyncio + requests"""

import asyncio
import requests
from bs4 import BeautifulSoup


url_list = [
    'https://github.com/explore',
    'https://www.djangoproject.com/',
    'http://www.python-requests.org/en/master/',
    'https://jquery.com/',
    'https://getbootstrap.com/',
    'https://www.solarwinds.com/',
    'https://www.zabbix.com/',
    'http://open-falcon.org/',
    'https://www.python.org/',
    'http://www.jetbrains.com/',
]


@asyncio.coroutine
def fetch_async(func, *args):
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(None, func, *args)
    response = yield from future
    response.encoding = 'utf-8'
    soup = BeautifulSoup(response.text, features='html.parser')
    title = soup.find('title')
    print(title)


if __name__ == '__main__':
    tasks = []
    for url in url_list:
        tasks.append(fetch_async(requests.get, url))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()

